package logic

import (
	"cart_srv/cart"
	"cart_srv/internal/svc"
	"cart_srv/model/mysql"
	"cart_srv/model/redis"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddGoodLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodLogic {
	return &AddGoodLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddGoodLogic) AddGood(in *cart.AddGoodRequest) (*cart.AddGoodResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Good{
		UserId:     int(in.UserId),
		GoodTitle:  in.GoodTitle,
		GoodStock:  int(in.GoodStock),
		GoodStatus: int(in.GoodStatus),
		GoodPrice:  in.GoodPrice,
	}
	err := g.Create()
	if err != nil {
		return nil, fmt.Errorf("添加失败")
	}
	redis.GoodStock(int64(g.ID), in.GoodStock)
	return &cart.AddGoodResponse{Success: true}, nil
}
